package com.jintoufs.web.action.sys;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.constants.MessageConstants;
import com.jintoufs.domain.org.Org;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.org.OrgService;
import com.jintoufs.util.ztree.TreeNode;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/org")
public class OrgAction extends SupportAction {

    @Resource
    private OrgService orgService;

    @RequestMapping("/list")
    public String list(Model model) throws IOException {
        List<Org> orgList = orgService.queryByParams(null);
        model.addAttribute("listEntity", orgList);
        return "org/list";
    }

    @RequestMapping("/getOrgList")
    @ResponseBody
    public List<Org> getOrgList() {
        List<Org> list = orgService.queryByParams(null);
        return list;
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "机构")
    @ResponseBody
    public AjaxMsg oper(Org org) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        try {
            if (org.getParentId() == null) {
                org.setParentId(0);
            }
            Map<String, Object> params = new HashMap();
            params.put("id", org.getParentId());
            List<Org> list = orgService.queryByParams(params);
            org.setStatus((byte) 1);
            if (list != null && list.size() > 0) {
                Org parent = list.get(0);
                org.setParentIds(parent.makeSelfAsParentIds());
            } else {
                org.setParentIds("0/");
            }
            if (org != null && org.getId() != null) {
                orgService.update(org, ajaxMsg);
            } else {
                orgService.add(org, ajaxMsg);
            }
        } catch (Exception e) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage(MessageConstants.SYSTEM_ERROR_MESSAGE);
        }
        return ajaxMsg;
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除机构")
    @ResponseBody
    public AjaxMsg delete(Integer id) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        try {
            orgService.deleteById(id, ajaxMsg);
        } catch (Exception e) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage(MessageConstants.SYSTEM_ERROR_MESSAGE);
        }
        return ajaxMsg;
    }

    @RequestMapping("input")
    public String input(Integer id, Model model) {
        Org entity = null;
        // 是否为编辑保存
        Boolean isEdit = false;

        // 修改
        if (id != null && id != 0) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", id);
            List<Org> list = orgService.queryByParams(params);
            if (list != null && list.size() > 0) {
                entity = list.get(0);
            }

            isEdit = true;
        } else {
            // 新增
        }
        List<TreeNode> orgTree = orgService.getOrgMenu(entity, 2, true);
        model.addAttribute("orgTree", JSON.toJSONString(orgTree));
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("entity", entity);

        return "org/input";
    }

}
